package com.jiangyg.mall.admin.controller;

import com.jiangyg.mall.admin.dto.PermissionDTO;
import com.jiangyg.mall.admin.dto.RoleDTO;
import com.jiangyg.mall.admin.vo.RoleVO;
import com.jiangyg.mall.admin.service.PermissionService;
import com.jiangyg.mall.admin.service.ResourceService;
import com.jiangyg.mall.admin.service.RoleService;
import com.jiangyg.mall.authz.UserInfoHolder;
import com.jiangyg.mall.core.enums.Enable;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.core.utils.tree.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：角色管理控制器
 *
 * @author jiangyg
 * @version 2.0
 * @date 2020-05-31
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    /**
     * 角色接口
     */
    private final RoleService roleService;

    /**
     * 资源接口
     */
    private final ResourceService resourceService;

    /**
     * 权限接口
     */
    private final PermissionService permissionService;

    @Autowired
    public RoleController(RoleService roleService, ResourceService resourceService,
                          PermissionService permissionService) {
        this.roleService = roleService;
        this.resourceService = resourceService;
        this.permissionService = permissionService;
    }

    /**
     * 功能描述：角色信息分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页角色信息
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<RoleVO>> select(HttpServletRequest request, @RequestBody Pagination<RoleVO> pagination) throws Exception {
        final Long userId = UserInfoHolder.loginUserId();
        return Result.ok(roleService.selectPagination(pagination, userId));
    }

    /**
     * 功能描述：保存或者修改角色信息
     *
     * @param request 请求
     * @param dto     角色信息
     * @return 结果
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated RoleDTO dto) throws Exception {
        final Long userId = UserInfoHolder.loginUserId();
        return Result.saveOrUpdate(roleService.saveOrUpdate(dto, userId));
    }

    /**
     * 功能描述：根据ID获取角色信息
     *
     * @param request 请求
     * @param roleId  角色ID
     * @return 结果
     * @throws Exception 异常
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<RoleVO> info(HttpServletRequest request, @RequestParam(name = "roleId") Long roleId) throws Exception {
        return Result.ok(roleService.selectById(roleId));
    }

    /**
     * 功能描述：删除角色信息
     *
     * @param request 请求
     * @param roleIds 批量删除的角色ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> roleIds) {
        return Result.delete(roleService.delete(roleIds));
    }

    /**
     * 功能描述：查询角色的权限信息
     *
     * @param request 请求
     * @param roleId  角色ID
     * @return 权限信息
     */
    @ResponseBody
    @PostMapping("/permission")
    public Result<Object> permission(HttpServletRequest request, @RequestParam("roleId") Long roleId) throws Exception {
        final Map<String, Object> data = new HashMap<>();
        final Long loginUserId = UserInfoHolder.loginUserId();
        // 查询用户所有的资源权限树
        final Long userId = UserInfoHolder.isAdmin() ? null : UserInfoHolder.loginUserId();
        final List<Tree> menus = resourceService.selectTree("2", userId);
        data.put("tree", menus);
        // 查询角色下所有的资源列表
        final List<String> resourceIds = roleService.selectResourcesById(roleId);
        data.put("ids", resourceIds);
        // 用户是否有修改权限，只能修改自己创建的角色
        final boolean have = roleService.haveUpdatePermission(roleId, loginUserId);
        data.put("enable", have ? Enable.Y.scode : Enable.N.scode);
        return Result.ok(data);
    }

    @ResponseBody
    @PostMapping("/save-permission")
    public Result<Object> savePermission(HttpServletRequest request, @RequestBody PermissionDTO dto) {
        final Long loginUserId = UserInfoHolder.loginUserId();
        return Result.saveOrUpdate(permissionService.saveRolePermission(dto, loginUserId));
    }

}
